package se.perp.c4.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import se.perp.c4.model.Actor;
import se.perp.c4.model.Actor.Side;
import se.perp.c4.model.ActorCollection;

public class EnemyReport extends JFrame  {

    private ActorCollection actorColl;
    private List<Actor> oppLiving = new ArrayList<Actor>();
    private JPanel southPanel;
    private JScrollPane jsp;
    private JButton okButton = new JButton("Close");
    private Side opponentSide;
    private Font descrFont = new Font(Font.SERIF, Font.BOLD, 14);
    private JFrame owner;

    public EnemyReport(JFrame owner, Side opponentSide, ActorCollection ac) {

        super("Enemies still alive");

        setLocationRelativeTo(null);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setIconImage(Images.get(Images.Which.APPICON));

        this.owner = owner;
        this.opponentSide = opponentSide;
        this.actorColl = ac;
        for (Actor a : actorColl) {
            if (a.side == opponentSide && a.isAlive) {
                oppLiving.add(a);
            }
        }
        Collections.sort(oppLiving);

        setLayout(new BorderLayout());

        okButton.setMnemonic('C');
        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        getRootPane().setDefaultButton(okButton);

        southPanel = new JPanel();
        southPanel.add(okButton);
        add(southPanel, BorderLayout.SOUTH);

        jsp = createView();
        add(jsp, BorderLayout.CENTER);

        setSize(300, owner.getHeight());
        setPreferredSize(getSize());

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle desktop = ge.getMaximumWindowBounds();
        Point p = owner.getLocationOnScreen();
        boolean moreSpaceToTheLeft = (p.x > (desktop.width - p.x - owner.getWidth()));
        int x;
        if (moreSpaceToTheLeft) {
            x = owner.getX() - getWidth() - 5;
        } else {
            x = owner.getX() + owner.getWidth() + 5;
        }
        setLocation(x, owner.getY());

        setVisible(true);
    }

    public void update() {
        oppLiving.clear();
        for (Actor a : actorColl) {
            if (a.isAlive && a.side == opponentSide) {
                oppLiving.add(a);
            }
        }
        Collections.sort(oppLiving);

        remove(jsp);
        jsp = createView();
        add(jsp, BorderLayout.CENTER);
        validate();
    }

    public void setOpponentSide(Side opponentSide) {
        this.opponentSide = opponentSide;
        update();
    }

    private JScrollPane createView() {
        JPanel p = new JPanel();
        p.setFocusable(false);
        int nLiving = oppLiving.size();
        p.setLayout(new GridLayout(9, 1));

        for (Actor a : oppLiving) {
            CharacterPanel cp = new CharacterPanel(a);
            p.add(cp);
        }
        for (int i = 0; i < 9 - nLiving; i++) {
            p.add(new BlackPanel());
        }

        JScrollPane sp = new JScrollPane(p);
        sp.setSize(p.getWidth(), 500);
        sp.setPreferredSize(getSize());
        sp.getVerticalScrollBar().setUnitIncrement(25);
        return sp;
    }

    private class BlackPanel extends JPanel {

        public BlackPanel() {
            setBackground(Color.BLACK);
        }
    }

    private class CharacterPanel extends JPanel {

        public CharacterPanel(Actor a) {
            setLayout(null);

            ImagePanel portrait = new ImagePanel(Portraits.getLabeledNumberedPortrait(a.who, a.name, a.attackValue));
            portrait.setLocation(2, 2);
            add(portrait);

            JTextPaneAA tp = new JTextPaneAA();
            tp.setEditable(false);
            tp.setBackground(null);
            tp.setOpaque(false);
            tp.setFont(descrFont);
            tp.setText(a.description);
            tp.setLocation(112, 2);
            tp.setSize(150, 100);
            tp.setHighlighter(null);
            add(tp);

            if (a.isDark()) {
                setBackground(ColorSet.DARKCARD_BG);
                tp.setForeground(ColorSet.DARKACTOR_TEXT);
                setBorder(BorderFactory.createLineBorder(ColorSet.DARKCARD_TEXT));
            } else {
                setBackground(ColorSet.LIGHTCARD_BG);
                tp.setForeground(ColorSet.LIGHTCARD_TEXT);
                setBorder(BorderFactory.createLineBorder(ColorSet.LIGHTCARD_TEXT));
            }

            setSize(portrait.getWidth() + tp.getWidth() + 14, portrait.getHeight() + 4);
            setPreferredSize(getSize());
        }
    }
}
